package cn.lf.android.lawfirm.undertake.apply.controler;


import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.distribution.modle.LawCase;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.undertake.apply.modle.UndertakeApply;
import cn.lf.android.lawfirm.undertake.approval.modle.CaseDetail;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.JsonBeans;
import cn.lf.android.lawfirm.utils.SPUtil;

public class ApplyUnderConroller extends BaseController {
    ApplyUnderInfoCallback mUnderListCallback;
    private Context context;
    UndertakeSubmitCallback mUndertakeSubmitCallback;

    public ApplyUnderConroller(ApplyUnderInfoCallback callback, UndertakeSubmitCallback undertakeSubmitCallback, Context context) {
        this.mUnderListCallback = callback;
        this.mUndertakeSubmitCallback = undertakeSubmitCallback;
        this.context = context;
    }

    /**
     */
    public void onApplyUnderInfo(String caseId) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("caseId", caseId);
        DebugLog.e("caseId==" + caseId);
        new MyOkHttpClient().doGet(AppConfig.CaseInfo_BasicInfo, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("承办申请信息beans==" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                responsObject = myJsonObject.getJSONObject("responsObject");
                                UndertakeApply beans = JSON.parseObject(responsObject.toString(), UndertakeApply.class);
                                DebugLog.e("beans==" + beans);
                                mUnderListCallback.onApplyUnderInfouccessed(beans);
                            } else {
                                mUnderListCallback
                                        .onApplyUnderInfoFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mUnderListCallback.onApplyUnderInfoFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    /**
     * @param caseid
     * @param approveman
     * @param applyForOpinion
     * @param level
     * @param basicFees
     * @param postStr
     * @param filesidStr
     * @param caseTypeID
     */
    public void onSubmit(String caseid, String approveman, String applyForOpinion, String level, String basicFees, String postStr, String filesidStr, String caseTypeID,String CaseOtherType) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("caseid", caseid);
        params.put("approveman", approveman);
        params.put("applyForOpinion", applyForOpinion);
        params.put("level", level);
        params.put("basicFees", basicFees);
        params.put("postStr", postStr);
        params.put("filesidStr", filesidStr);
        params.put("caseTypeID", caseTypeID);
        params.put("CaseOtherType", CaseOtherType);

        DebugLog.e("caseidc==" + caseid);
        DebugLog.e("approvemanc==" + approveman);
        DebugLog.e("applyForOpinionc==" + applyForOpinion);
        DebugLog.e("levelc==" + level);
        DebugLog.e("basicFeesc==" + basicFees);
        DebugLog.e("postStrc=="+postStr);
        DebugLog.e("filesidStrc=="+filesidStr);
        DebugLog.e("caseTypeIDc=="+caseTypeID);
        DebugLog.e("CaseOtherTypec==" + CaseOtherType);
        new MyOkHttpClient().doPost(AppConfig.Apply_ApplyForCase, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("承办申请信息beansc==" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                mUndertakeSubmitCallback.onSubmitSuccessed("");
                            } else {
                                mUndertakeSubmitCallback
                                        .onSubmitFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mUndertakeSubmitCallback.onSubmitFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    public interface ApplyUnderInfoCallback {
        public void onApplyUnderInfouccessed(UndertakeApply beans);

        public void onApplyUnderInfoFailed();

    }

    public interface UndertakeSubmitCallback {
        public void onSubmitSuccessed(String str);

        public void onSubmitFailed();

    }
}
